{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6-final"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "orgDf=pd.read_csv(\"data/OrgName.csv\")\n",
    "# orgDf.drop(\"Unnamed: 0\",axis=1,inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "  OrgName\n0     办公室\n1    宣传中心\n2     组织部\n3  志愿服务中心\n4     实践部",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>OrgName</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>办公室</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>宣传中心</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>组织部</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>志愿服务中心</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>实践部</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 2
    }
   ],
   "source": [
    "orgDf.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "#oversampling\n",
    "orgList=orgDf[\"OrgName\"].values.tolist()\n",
    "\n",
    "import numpy as np\n",
    "overSampleList=[]\n",
    "for row in orgList:\n",
    "    for overItem in range(10):\n",
    "        randNum=np.random.random()\n",
    "        if randNum>0.7:\n",
    "            startI=0\n",
    "            endI=startI+2+int(np.random.random()*(len(row)-startI))\n",
    "            overSampleList.append((row,row[startI:endI],1))\n",
    "        else:\n",
    "            startI=int(np.random.random()*(len(row)*0.3))+1\n",
    "            endI=startI+1+int(np.random.random()*(len(row)-startI))\n",
    "            overSampleList.append((row,row[:startI]+row[endI:],1))\n",
    "\n",
    "overSampleList=pd.DataFrame(overSampleList).drop_duplicates().values.tolist()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "#undersampling\n",
    "orgList=orgDf[\"OrgName\"].values.tolist()\n",
    "\n",
    "import numpy as np\n",
    "underSampleList=[]\n",
    "for row in orgList:\n",
    "    for overItem in range(10):\n",
    "        newRow=orgList[int(np.random.random()*len(orgList))]\n",
    "        randNum=np.random.random()\n",
    "        if randNum>0.7:\n",
    "            startI=0\n",
    "            endI=startI+2+int(np.random.random()*(len(newRow)-startI))\n",
    "            overSampleList.append((row,newRow[startI:endI],0))\n",
    "        else:\n",
    "            startI=int(np.random.random()*(len(newRow)*0.3))+1\n",
    "            endI=startI+1+int(np.random.random()*(len(newRow)-startI))\n",
    "            overSampleList.append((row,newRow[:startI]+newRow[endI:],0))\n",
    "\n",
    "underSampleList=pd.DataFrame(underSampleList).drop_duplicates().values.tolist()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "#combine samples\n",
    "sampleList=overSampleList+underSampleList"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pickle as pkl\n",
    "with open(\"data/orgSynSampleList.pkl\",\"wb+\") as orgSynSampleListFile:\n",
    "    pkl.dump(sampleList,orgSynSampleListFile)"
   ]
  }
 ]
}